$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'sys/intelreplen/list',
        datatype: "json",
        colModel: [			
			{ label: 'id', name: 'id', index: 'id', width: 50, key: true,hidden:true },
			{ label: '商品图片', name: 'productimg', index: 'productimg', width: 80,formatter: function(cellvalue, options, rowdata) {
                    if(rowdata.productimg!=null&&rowdata.productimg!=''){
                        return '<img src='+rowdata.productimg+' width="50" height="50"/>';
                    }else{
                        return '';
                    }

                }  },
			{ label: '商品名称', name: 'productname', index: 'productname', width: 80 },
			{ label: '商品分类', name: 'categoryid', index: 'categoryid', width: 80,formatter: function(cellvalue, options, rowdata) {
                    if(rowdata.categoryid!=null&&rowdata.categoryid!=""){
                        var groupname = "";
                        var arr = rowdata.categoryid.split(",");
                        for(var i=0;i<arr.length;i++){
                            $.ajax({
                                type: "POST",
                                async:false,
                                url: baseURL + "sys/productcategory/info/"+parseInt(arr[i]),
                                success: function(r){
                                    groupname +=r.productcategory.name;
                                    if(i !=arr.length-1){
                                        groupname +="/";
                                    }

                                }
                            });
                        }
                        return groupname;
                    }else{
                        return '未分组';
                    }


                }},
            { label: '库存下限', name: 'lowerLimit', index: 'lowerLimit', width: 80 },
            { label: '库存上限', name: 'upperLimit', index: 'upperLimit', width: 80 },
            { label: '当前库存', name: 'stock', index: 'stock', width: 80 },
			{ label: '待上架数量', name: 'onshelves', index: 'onshelves', width: 80 },
			{ label: '建议采购量', name: 'buycount', index: 'buycount', width: 80 },
            { label: '最近成本价', name: 'avgcost', index: 'avgcost',width: 70},
            { label: '成本总额', name: 'totalcost', index: 'totalcost',width: 70}
        ],
		viewrecords: true,
        height: 385,
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });
    vm.Statistics(null,null,null);
    vm.queryCategory();
    vm.queryGoodshelves();
});

var vm = new Vue({
	el:'#rrapp',
	data:{
		q:{
            name:null,
            shelvesid:null,
            categoryid:null

		},
		showList: true,
        showInfoList :true,
		title: null,
        intelreplen: {}
	},
	methods: {
		query: function () {
			vm.reload();
		},
		add: function(){
			vm.showList = false;
            vm.showInfoList = false;
			vm.intelreplen = {};
		},
		update: function (event) {
			var id = getSelectedRow();
			if(id == null){
				return ;
			}
			vm.showList = false;
            vm.title = "修改";
            
            vm.getInfo(id)
		},
        onshelevesExcel: function () {
            $('#intelreplenModal').modal();
        },
		saveOrUpdate: function (event) {
			var url = vm.lowershelves.id == null ? "sys/lowershelves/save" : "sys/lowershelves/update";
			$.ajax({
				type: "POST",
			    url: baseURL + url,
                contentType: "application/json",
			    data: JSON.stringify(vm.lowershelves),
			    success: function(r){
			    	if(r.code === 0){
						alert('操作成功', function(index){
							vm.reload();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
        Statistics: function (shelvesid,name,categoryid) {
            $.ajax({
                type: "POST",
                async:false,
                url: baseURL + "sys/intelreplen/infoStatistics",
                data:{'shelvesid':shelvesid,'name':name,'categoryid':categoryid},
                success: function(r){
                    $("#spzl").text(r.intelreplen.count);
                    $("#jycgl").text(r.intelreplen.buycount==null?0:r.intelreplen.buycount);
                    $("#hjcbje").text(r.intelreplen.totalcost==null?"￥"+0:"￥"+r.intelreplen.totalcost);

                }
            });
        },
        exports: function () {
		    var data = {"shelvesid":vm.q.shelvesid,"name":vm.q.name,"categoryid":vm.q.categoryid};

            $.ajax({
                type: "POST",
                async:false,
                url: baseURL + "sys/intelreplen/exportIntelReplen",
                contentType : "application/json",
                dataType:"json",
                data:JSON.stringify(data),
                success: function(r){
                    alert(r);

                }
            });

        },
        addIntelreplen: function () {
            var bc = $("input:radio[name='intelreplen']");
            for(var i=0;i<bc.length;i++){
                if(bc[i].value == 1){//导出excel
                    vm.exports();
                }
            }
            var data = {"shelvesid":vm.q.shelvesid,"name":vm.q.name,"categoryid":vm.q.categoryid};

            $.ajax({
                type: "POST",
                async:false,
                url: baseURL + "sys/intelreplen/onShelves",
                contentType : "application/json",
                dataType:"json",
                data:JSON.stringify(data),
                success: function(r){
                    if(r.code === 0){
                        alert('操作成功', function(index){
                            vm.reload();
                        });
                    }else{
                        alert(r.msg);
                    }

                }
            });
            $('#intelreplenModal').modal("hide");
        },
		del: function (event) {
			var ids = getSelectedRows();
			if(ids == null){
				return ;
			}
			
			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: baseURL + "sys/lowershelves/delete",
                    contentType: "application/json",
				    data: JSON.stringify(ids),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
        queryCategory: function(){
            $("#sheelvesgroup_id").html("");
            $.get(baseURL + "sys/productcategory/alllist", function(r){
                var temp = "<option value=''></option><option value='0'>未分组</option>";
                $.each(r.alllist, function (i) {

                    temp +="<option value='"+r.alllist[i].id+"'>"+r.alllist[i].name+"</option>";

                });
                $("#sheelvesgroup_id").append(temp);

            });
        },
        queryGoodshelves: function(){
            $("#sheelves_id").html("");
            $.get(baseURL + "sys/goodshelves/alllist", function(r){
                var temp = "<option value=''></option>";
                $.each(r.alllist, function (i) {

                    temp +="<option value='"+r.alllist[i].id+"'>"+r.alllist[i].shelvesname+"</option>";

                });
                $("#sheelves_id").append(temp);

            });
        },
		getInfo: function(id){
			$.get(baseURL + "sys/intelreplen/info/"+id, function(r){
                vm.lowershelves = r.lowershelves;
            });
		},
		reload: function (event) {
			vm.showList = true;
			vm.showInfoList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{
                postData:{'shelvesid':vm.q.shelvesid,'name':vm.q.name,'categoryid':vm.q.categoryid},
                page:page
            }).trigger("reloadGrid");
            vm.Statistics(vm.q.shelvesid,vm.q.name,vm.q.categoryid);
		}
	}
});